package com.kggs.modbus4jgateway;

import com.kggs.modbus4jgateway.bean.Master;
import com.kggs.modbus4jgateway.bean.Slave;
import com.kggs.modbus4jgateway.bean.SlavePoint;
import com.kggs.modbus4jgateway.bean.SlaveWrite;
import com.kggs.modbus4jgateway.factory.WriteHelper;
import com.kggs.modbus4jgateway.service.IModbus4JMasterService;
import com.kggs.modbus4jgateway.service.IModbus4jReadService;
import com.kggs.modbus4jgateway.utils.SpringContextUtil;
import com.serotonin.modbus4j.code.DataType;

import java.util.ArrayList;
import java.util.List;

public class RunTask implements Runnable {
    @Override
    public void run() {
        //多网关模式
        //第一步、启动并连接所有的Master
        IModbus4JMasterService masterService = SpringContextUtil.getBean(IModbus4JMasterService.class);
        Master master = new Master("192.168.0.10");
        List<Master> masters = new ArrayList<>();
        masters.add(master);
        masterService.Start(masters);
        //第二步、初始化SlaveId与点位寄存器地址，并与Master绑定
        List<Slave> slaveList = new ArrayList<>();
        Slave slave = null;
        //int _hex = IntegerUtil.ConvertBy16Hex("0x0A");
        //热泵
        slave = new Slave(master, 1, 0x03, new SlavePoint(1506, DataType.TWO_BYTE_INT_UNSIGNED, "供水温度"));
        //        slaveList.add(slave);
        //        slave = new Slave(master, 1, 0x03, new SlavePoint(1505, DataType.TWO_BYTE_INT_UNSIGNED, "回水温度"));
        //        slaveList.add(slave);
        //        slave = new Slave(master, 1, 0x03, new SlavePoint(1606, DataType.TWO_BYTE_INT_UNSIGNED, "制热启动温度"));
        //        slaveList.add(slave);
        //        slave = new Slave(master, 1, 0x03, new SlavePoint(1607, DataType.TWO_BYTE_INT_UNSIGNED, "制热停止温度"));
        //        slaveList.add(slave);
        //        slave = new Slave(master, 1, 0x03, new SlavePoint(1675, DataType.TWO_BYTE_INT_UNSIGNED, "热风启动温度"));
        //        slaveList.add(slave);
        //        slave = new Slave(master, 1, 0x03, new SlavePoint(1676, DataType.TWO_BYTE_INT_UNSIGNED, "热风停止温度"));
        //        slaveList.add(slave);

        //slave = new Slave(master, 92, 0x03, new SlavePoint(50, DataType.FOUR_BYTE_FLOAT, "电能"));
        //slaveList.add(slave);
//        slave = new Slave(master, 1, 0x03, new SlavePoint(4097, DataType.TWO_BYTE_INT_SIGNED, "电能"));
//        slaveList.add(slave);
//        slave = new Slave(master, 2, 0x03, new SlavePoint(50, DataType.FOUR_BYTE_FLOAT, "电能"));
//        slaveList.add(slave);
        //第三步、开启读线程，读取点位数据
        //IModbus4jReadService salveService = SpringContextUtil.getBean(IModbus4jReadService.class);
        // salveService.Start(slaveList);
        //WriteHelper.Write(new SlaveWrite<>(master, 50, 43081, true));
//        Integer _short = 400;
//        for (int i = 0; i < 100; i++) {
//            WriteHelper.Write(new SlaveWrite<Integer>(master, 1, 1606, _short, DataType.TWO_BYTE_INT_SIGNED));
//            try {
//                Thread.sleep(1000);
//            } catch (InterruptedException e) {
//                e.printStackTrace();
//            }
//        }
    }
}
